﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using GrapeCity.ActiveAnalysis.Windows.Forms;

namespace GrapeCity.ActiveAnalysis.Samples
{
	internal static class SamplesImpl
	{
		public static void LoadLayout(XmlNode docElement, PivotView pivotView)
		{
			pivotView.BeginInit();
			pivotView.Cursor = Cursors.WaitCursor;
			try
			{
				// Do not close old data source to improve the speed of reloading.

				pivotView.DataSource = DataSourceManager.ReadDataSource(docElement);
				if (!pivotView.DataSource.IsConnected)
					pivotView.DataSource.Connect();

				using (XmlNodeReader reader = new XmlNodeReader(docElement))
					pivotView.Read(reader, PersistSettings.Layout | PersistSettings.CardLayout);
			}
			catch (Exception ex)
			{
				ShowMessageBox(ex, MessageBoxIcon.Error, MessageBoxButtons.OK, pivotView);
			}
			finally
			{
				pivotView.Cursor = Cursors.Arrow;
				pivotView.EndInit();
			}
		}

		[System.Runtime.InteropServices.DllImport("Shfolder.dll")]
		private static extern int SHGetFolderPath(IntPtr hwndOwner, int nFolder, IntPtr hToken, int dwFlags, StringBuilder lpszPath);
		public static string MyDocumentsPath
		{
			get
			{
				StringBuilder comDoc = new StringBuilder(260);
				SHGetFolderPath(IntPtr.Zero, 0x002e, IntPtr.Zero, 0, comDoc);
				return comDoc.ToString();
			}
		}

		public static DialogResult ShowMessageBox(Exception ex, MessageBoxIcon messageBoxIcon, MessageBoxButtons buttons,
			Control parentControl)
		{
			while (ex.InnerException != null)
				ex = ex.InnerException;
			return ShowMessageBox(ex.Message, messageBoxIcon, buttons, parentControl);
		}

		public static DialogResult ShowMessageBox(string message, MessageBoxIcon messageBoxIcon, MessageBoxButtons buttons,
			Control parentControl)
		{
			MessageBoxOptions options = 0;

			RightToLeft rightToLeftValue = RightToLeft.Inherit;
			if (parentControl != null)
			{
				rightToLeftValue = parentControl.Parent == null ? parentControl.RightToLeft : parentControl.Parent.RightToLeft;
				Control parent = parentControl.Parent;

				while ((rightToLeftValue == RightToLeft.Inherit) && (parent != null))
				{
					parent = parent.Parent;
					rightToLeftValue = parent.RightToLeft;
				}
			}

			if (rightToLeftValue == RightToLeft.Yes)
			{
				options = MessageBoxOptions.RtlReading | MessageBoxOptions.RightAlign;
			}

			return MessageBox.Show
				(
				parentControl,
				message,
				Application.ProductName,
				buttons,
				messageBoxIcon,
				MessageBoxDefaultButton.Button1,
				options
				);
		}

		/// <summary>
		/// Special HACK method.
		/// Note: it is not a part of the public API and can be changed in future.
		/// </summary>
		/// <param name="pivotView"></param>
		/// <returns></returns>
		public static bool GetResetGridState(PivotView pivotView)
		{
			object settings = pivotView.GetType().GetProperty("ViewSettings", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(pivotView, null);
			settings = settings.GetType().GetProperty("Grid", BindingFlags.Public | BindingFlags.Instance).GetValue(settings, null);
			IDictionary<int, int> columnWidths = settings.GetType().GetProperty("ColumnWidths", BindingFlags.Public | BindingFlags.Instance).GetValue(settings, null) as IDictionary<int, int>;
			IDictionary<int, int> rowHeights = settings.GetType().GetProperty("RowHeights", BindingFlags.Public | BindingFlags.Instance).GetValue(settings, null) as IDictionary<int, int>;
			int? cellWidth = settings.GetType().GetProperty("CellWidth", BindingFlags.Public | BindingFlags.Instance).GetValue(settings, null) as int?;
			int? cellHeight = settings.GetType().GetProperty("CellHeight", BindingFlags.Public | BindingFlags.Instance).GetValue(settings, null) as int?;
			
			bool canResetGrid = 
					cellWidth.HasValue ||
					cellHeight.HasValue ||
					(columnWidths != null && columnWidths.Count > 0) ||
					(rowHeights != null && rowHeights.Count > 0);
			
			return canResetGrid;
		}
	}
}